package main

import (
	"fmt"
	"godb/config"

	"github.com/gin-gonic/gin"
)

type Order struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

func main() {

	r := gin.Default()

	//查询
	r.GET("/ca1", func(c *gin.Context) {
		var order Order
		_, err := config.XormDb.Table("user").Where("id = ?", 123).Get(&order)
		if err != nil {
			fmt.Println("数据库user查询失败", err)
		}
		fmt.Println(order)
	})
	r.GET("/ca2", func(c *gin.Context) {
		var orders []Order
		err := config.XormDb.Table("user").Where("name = ?", "alsark").Find(&orders)
		if err != nil {
			fmt.Println("数据库user查询失败", err)
		}
		fmt.Println(orders)
	})

	//增加
	r.GET("/add", func(c *gin.Context) {
		var order Order
		_, err := config.XormDb.Table("user").Insert(&order)
		if err != nil {
			fmt.Println("数据库user插入失败", err)
		}
	})

	//删除
	_, err := config.XormDb.Exec("DELETE FROM user WHERE id = ?", 1)
	if err != nil {
		fmt.Println("数据库user删除失败", err)
	}

	//更新
	_, err1 := config.XormDb.Exec("UPDATE user SET name = ?,age = ? WHERE id = ?", "xiaoming", 13, 1)
	if err1 != nil {
		fmt.Println("数据库user更新失败", err1)
	}

	r.Run()

}
